﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartMD.DataAccess.Interface.Dao;
using SmartMD.DataModel.Entity;
using System.Data;
using SmartMD.DataAccess.Service;
using System.Collections.ObjectModel;

namespace SmartMD.DataAccess.Oracle.DaoImpl
{
    public class ExamItemDaoImpl : BaseGenericDaoImpl, IExamItemDao 
    {
        public bool DeleteObject(Object entity)
        {
            ExamItemInfo eItem = (ExamItemInfo)entity;
            StringBuilder sb = new StringBuilder();
            List<IndiceDefine> indiceList = new List<IndiceDefine>();
            IndiceDefine indice;
            for (int j = 0; j < eItem.columns.Count; j++)
            {
                indice = eItem.columns[j];
                indice.ItemId = eItem.Id;
                DataServiceManager.IndiceDefineSevice.DeleteObject(indice);
            }            

            sb.Clear();
            sb.Append(" delete from DUO_EXAMINATION_ITEM");
            sb.Append(" where ITEMID ='" + eItem.Id + "'");

             this.dbConn.Delete(sb.ToString());
             return true;
        }

        public bool SaveOrUpdateObject(Object entity)
        {
            //删除对象
            DeleteObject(entity);
            //新增对象
            ExamItemInfo eItem = (ExamItemInfo)entity;
            StringBuilder sb = new StringBuilder();

            List<IndiceDefine> indiceList = new List<IndiceDefine>();
            IndiceDefine indice;
            for (int j = 0; j < eItem.columns.Count; j++)
            {
                indice = eItem.columns[j];
                indice.ItemId = eItem.Id;
                indiceList.Add(indice);
            }
            bool isIndiceSaved = DataServiceManager.IndiceDefineSevice.saveIndiceDefines(indiceList);

            sb.Clear();
            sb.Append(" insert into");
            sb.Append(" DUO_EXAMINATION_ITEM (ITEMID, NAME, STORETABLENAME, LASTMODIFY, SOURCEITEMID, ITEMTYPE, ISENABLED)");
            sb.Append(" values(");
            sb.Append(" '" + eItem.Id + "',");
            sb.Append(" '" + eItem.Name + "',");
            sb.Append(" '" + eItem.Description + "',");
            sb.Append(" to_date ('" + eItem.LastModify.ToString("yyyy-MM-dd HH:mm:ss") + "' , 'YYYY-MM-DD HH24:MI:SS' ) ,");
            sb.Append(" '" + eItem.SourceItemId + "',");
            sb.Append(" '" + eItem.ItemType + "',");
            sb.Append(" '" + eItem.IsEnabled.ToString() + "'");
            sb.Append(")");
            bool isExamItemSaved = this.dbConn.Insert(sb.ToString());

            return isIndiceSaved & isExamItemSaved;
        }
        public ObservableCollection<ExamItemInfo> loadAllExamItemInfo(bool getEnabled = false)
        {
            ObservableCollection<ExamItemInfo> examItems = new ObservableCollection<ExamItemInfo>();
            ExamItemInfo item;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_EXAMINATION_ITEM");
            if (getEnabled)
            {
                sb.Append(" where ISENABLED = 'True'");
            }

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    item = new ExamItemInfo();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        item.setValue(clmName, dt.Rows[j][clmName]);
                    }
                    ObservableCollection<IndiceDefine> indiceDefineList = DataServiceManager.IndiceDefineSevice.loadIndiceDefineById(item.Id);
                    item.columns = indiceDefineList;
                    examItems.Add(item);
                }
                return examItems;
            }
            else
                return null;          
        }

        public ExamItemInfo loadExamItemInfoById(string id)
        {
            ExamItemInfo item = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_EXAMINATION_ITEM");
            sb.Append(" where ITEMID ='" + id + "'");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    item = new ExamItemInfo();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        item.setValue(clmName, dt.Rows[j][clmName]);
                    }
                    ObservableCollection<IndiceDefine> indiceDefineList = DataServiceManager.IndiceDefineSevice.loadIndiceDefineById(item.Id);
                   item.columns = indiceDefineList;                  
                }
                return item;
            }
            else
                return null;          
        }
        public List<ExamItemInfo> loadExamItemInfoByNames(List<String> nameLst)
        {
            throw new NotImplementedException();
        }
        public bool saveExamItems(List<ExamItemInfo> examItems)
        {
            bool isSaved = true;
            ExamItemInfo eItem;
            
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < examItems.Count; i++)
            {
                eItem = examItems[i];
               bool success = SaveOrUpdateObject(eItem);
               isSaved = isSaved & success;
            }
            return isSaved;
        }
    }
}
